from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet,ReadOnlyModelViewSet

from goods.models import SPU, Brand,GoodsChannel,GoodsCategory
from meiduo_admin.utils import PageNum
from meiduo_admin.serializers.spus import SPUSerializer,BrandSerializer,CategoriesSerializer


class SPUView(ModelViewSet):
    serializer_class = SPUSerializer
    pagination_class = PageNum
    queryset = SPU.objects.all()
    permission_classes = [IsAdminUser]

    def simple(self,request):
        # 品牌选项
        brands = Brand.objects.all()
        ser = BrandSerializer(brands, many=True)

        return Response(ser.data)

    def channel1(self, request):
        # 频道选项
        # channels = GoodsChannel.objects.all()
        cat1 = GoodsCategory.objects.filter(parent__isnull=True)
        ser = CategoriesSerializer(cat1, many=True)

        return Response(ser.data)

    def channel2(self, request, pk):
        # 自定义方法查询子集商品
        data = GoodsCategory.objects.get(parent=pk)

        ser = CategoriesSerializer(data, many=True)

        return Response(ser.data)



